// index.ts
// 获取应用实例
import {REQUEST} from "miniprogram-request";

// const app = getApp<IAppOption>();

Page({
    data: {
        cardCur     : 0,
        swiperList  : [],
        personalized: [],
        newest      : [],
        newSongs    : [],
        gridCol     : 4,
        iconList    : [
            {
                icon : 'tuijian',
                color: 'red',
                badge: 0,
                name : '今日推荐'
            },
            {
                icon : 'gedan',
                color: 'orange',
                badge: 0,
                name : '歌单'
            },
            {
                icon : 'paixing',
                color: 'yellow',
                badge: 0,
                name : '排行'
            },
            {
                icon : 'yinle',
                color: 'olive',
                badge: 0,
                name : '无损'
            }
        ]
    },
    onLoad(): void
    {
    },
    onShow(): void
    {
        this.getBanners();
        this.getNewest();
        this.getNewSongs();
        this.getPersonalized()
    },
    onUnload(): void
    {
    },
    onHide(): void
    {
    },
    onReady(): void
    {
    },

    cardSwiper(e: any)
    {
        this.setData({
            cardCur: e.detail.current
        })
    },

    openNewView(e: any)
    {
        const id = e.currentTarget.dataset.id;
        console.log(id);
        switch (id)
        {
            case 0:
                wx.navigateTo({
                    url: '../newsong/newsong',
                });
                break;
            case 1:
                wx.navigateTo({
                    url: '../playlist/playlist',
                });
                break;
            case 2:
                wx.navigateTo({
                    url: '../ranking/ranking',
                });
                break;
            case 3:
                wx.navigateTo({
                    url: '../ape-flac/ape-flac',
                });
                break;
        }
    },

    getBanners()
    {
        REQUEST.get("banner").then((value: any) =>
        {
            if (value.data.code == 200)
            {
                this.setData({
                    swiperList: value.data.banners
                })
            }
        })
    },
    getNewest()
    {
        REQUEST.get("album/newest").then(value =>
        {
            const res: any = value.data;
            console.log(res.albums);
            if (res.code == 200)
            {
                console.log(res.albums);
                this.setData({
                    newest: res.albums
                })
            }
        })
    },
    getPersonalized()
    {
        REQUEST.get("personalized?limit=6").then(value =>
        {
            const res: any = value.data;
            if (res.code == 200)
            {
                const pageData: [] = res.result;
                pageData.forEach((item: any) => item.playCount = (item.playCount / 10000).toFixed(0))
                this.setData({
                    personalized: pageData
                })
            }
        })
    },
    getNewSongs()
    {
        REQUEST.get("personalized/newsong").then(value =>
        {
            const res: any = value.data;
            if (res.code == 200)
            {
                this.setData({
                    newSongs: res.result
                })
            }
        })
    },
    openSearch()
    {
        wx.navigateTo({
            url: '../search/search',
        });
    }
});
